<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/5/29
 * Time: 12:45
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\BusSkd;
use think\Error;

class BusSkdLogic extends BaseLogic
{

    private $busSkdModel;

    public function __construct()
    {
        parent::__construct();
        $this->busSkdModel = new BusSkd();
    }

    public function info($skdId){
        if ($skdId){
            $info = $this->busSkdModel->get($skdId);
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '请选择正确的排班');
        }
    }

    public function busSkdList($tempWhere, $nowPage){
        $where = [];
        if (is_numeric($tempWhere['type'])){
            $where[] = ['bs.type', '=', $tempWhere['type']];
        }
        if (is_numeric($tempWhere['status'])){
            $where[] = ['bs.status', '=', $tempWhere['status']];
        }
        if ($tempWhere['bus_no']){
            $where[] = ['b.bus_no', 'like', "%".$tempWhere['bus_no']."%"];
        }
//        if ($tempWhere['business_name']){
//            $where[] = ['bb.name', 'like', "%".$tempWhere['business_name']."%"];
//        }
        if ($tempWhere['business_id']){
            $where[] = ['bs.business_id', '=', $tempWhere['business_id']];
        }
        $count = $this->busSkdModel->alias('bs')->field('bs.*,b.bus_no,bb.name')->
                        leftJoin('bus b','bs.bus_id = b.id')->
                        leftJoin('bus_business bb', 'bb.id = bs.business_id')->
                        where($where)->count();
        if ($count > 0){
            $list = $this->busSkdModel->alias('bs')->field('bs.*,b.bus_no,bb.name')->
                        leftJoin('bus b','bs.bus_id = b.id')->
                        leftJoin('bus_business bb', 'bb.id = bs.business_id')->
                        where($where)->page($nowPage, self::$pageNum)->order('bs.id desc')->all();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                $dateRange = $data['skd_time_range'];
                $dateRange = explode(' - ', $dateRange);
                $firstData['business_id'] = $data['business_id'];
                $firstData['bus_id'] = $data['bus_id'];
                $firstData['status'] = $data['status'] ? 1:0;
                foreach ($data['skd_date'] as $key=>$val){
                    $temp = $firstData;
                    list($begin,$end) = self::timeChange($val);
                    $temp['skd_date'] = $begin;
                    $temp['end_date'] = $end;
                    $temp['begin_city'] = $data['begin_city'][$key];
                    $temp['end_city'] = $data['end_city'][$key];
                    $temp['price'] = $data['price'][$key];
                    $allData[] = $temp;
                }
                $res = $this->busSkdModel->saveAll($allData);
                break;
            case 'edit':
                $res = $this->busSkdModel->allowField(true)->save($data, ['id' => $data['id']]);
                break;
            case 'del':
                $busSkd = $this->busSkdModel->get($data['id']);
                $res = $busSkd->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

    private static function timeChange($day){
        $begin = $day;
        $end = $day.'23:59:59';
        return [$begin,$end];
    }

}